package net.qiqbframework.commandhandling;


import net.qiqbframework.messaging.GenericResultMessage;
import net.qiqbframework.messaging.Message;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;

public class GenericCommandResultMessage<R> extends GenericResultMessage<R> implements CommandResultMessage<R> {


    @SuppressWarnings("unchecked")
    public static <T> CommandResultMessage<T> asCommandResultMessage(@Nonnull Object commandResult) {
        if (commandResult instanceof CommandResultMessage) {
            return (CommandResultMessage<T>) commandResult;
        } else if (commandResult instanceof Message) {
            Message<T> commandResultMessage = (Message<T>) commandResult;
            return new GenericCommandResultMessage<>(commandResultMessage);
        }
        return new GenericCommandResultMessage<>((T) commandResult);
    }

    public static <T> CommandResultMessage<T> asCommandResultMessage(@Nonnull Throwable exception) {
        return new GenericCommandResultMessage<>(exception);
    }

    public GenericCommandResultMessage(@Nullable R commandResult) {
        super(commandResult);
    }


    public GenericCommandResultMessage(@Nonnull Throwable exception) {
        super(exception);
    }


    public GenericCommandResultMessage(@Nonnull R commandResult, @Nonnull Map<String, ?> metaData) {
        super(commandResult, metaData);
    }

    public GenericCommandResultMessage(@Nonnull Throwable exception, @Nonnull Map<String, ?> metaData) {
        super(exception, metaData);
    }

    public GenericCommandResultMessage(@Nonnull Message<R> delegate) {
        super(delegate);
    }


    public GenericCommandResultMessage(@Nonnull Message<R> delegate, @Nullable Throwable exception) {
        super(delegate, exception);
    }

    @Override
    protected String describeType() {
        return "GenericCommandResultMessage";
    }
}
